package leveldesigner.gui.main;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import leveldesigner.gui.map.NewMap;
import leveldesigner.map.Map;
import leveldesigner.util.Resources;

@SuppressWarnings("serial")
public class MainWindow extends JFrame implements ActionListener{
	private JPanel panel;
	private JMenuItem itemNew;
	private JMenuItem itemOpen;
	private File openedFile;
	
	public void create(){
		panel = new JPanel();
		JMenuBar menu= new JMenuBar();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		openedFile = null;
		
		this.createMenu(menu);
		this.createPanel(panel);
		
		this.setJMenuBar(menu);
		this.add(panel);
		
		this.setSize(Resources.INITIAL_WIDTH, Resources.INITIAL_HEIGHT);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void createMenu (JMenuBar menu){
		JMenu menuFile = new JMenu ("File");
		menu.add(menuFile);
		itemNew = new JMenuItem ("New Map");
		itemNew.addActionListener(this);
		menuFile.add(itemNew);
		itemOpen = new JMenuItem ("Open Map");
		menuFile.add(itemOpen);
	}
	public MainWindow(){
		super(Resources.APP_NAME);
	}
	
	private void createPanel (JPanel panel){
		// Grid Layout just to the components fill the full screen
		panel.setLayout(new GridLayout(1,1));
		// Scroll Panes for add the components with the tileSet, map and the layers
		JScrollPane tileSetSPane = new JScrollPane();
		JScrollPane mapSPane = new JScrollPane();
		JScrollPane layerSPane = new JScrollPane();
		// SplitPane with the tileset and map scroll panes
		JSplitPane mapAndLayerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tileSetSPane, mapSPane);
		mapAndLayerPane.setOneTouchExpandable(true);
		mapAndLayerPane.setDividerLocation(250);
		// SplitPane with all the scrollpanes
		JSplitPane backPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT ,mapAndLayerPane,layerSPane);
		backPane.setOneTouchExpandable(true);
		backPane.setDividerLocation(400);
		panel.add(backPane);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == itemNew){
			NewMap newMapWindow = new NewMap(this);
			if (newMapWindow.isMapCreated()){
				Map map = new Map(newMapWindow.getMapWidth(), newMapWindow.getMapHeight(), newMapWindow.getMapName());
				saveMap (map, openedFile);
			}
		}
	}
	
	public void saveMap(Map map, File file){
		boolean newFile = false;
		if (file == null){
			try {
				String path = System.getProperty("user.home")+"/Maps";
				File dir = new File(path);
				dir.mkdir();
				file = new File (path+"/"+map.getName()+".map");
				file.createNewFile();
			} catch (IOException e) {
				JOptionPane.showMessageDialog (this, "Unable to create the file.", "Error", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
				return;
			}
			newFile = true;
		}
		
		try {
			ObjectOutputStream objectOut = new ObjectOutputStream (new BufferedOutputStream(new FileOutputStream(file)));
			objectOut.writeObject(map);
			objectOut.close();
			if (!newFile){
				JOptionPane.showMessageDialog (this, "Map saved successful.", "Map saved", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog (this, "File not found.", "Error", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
